import cSign from './sign'

export default class resReport{
    constructor({recode,remsg,reqseq,sign,result,d}){
        this.reCode=recode
        this.reMsg=remsg
        this.reqSeq=reqseq
        this.sign=sign
        this.result=result
        this.data = d
    }
    checkSign(){
        alert(toString());
        if(this.sign==getSign(toString())){
            return 0;
        }else{
            this.reCode="9999";
            this.reMsg="签名校验失败";
            return 1;
        }
    }
    getReCode(){
        return this.reCode;
    }
    getReMsg(){
        return this.reMsg;
    }
    getReqSeq(){
        return this.reqSeq;
    }
    toString(){
        //return "{\"reCode\":\""+ this.reCode+"\",\"reMsg\":\""+this.reMsg+"\",\"reqSeq\":\""+this.reqSeq+"\",\"sign\":\"\",\"result\":\""+ this.result + "\",\"data\":"+JSON.stringify(data)+"}";
        alert(JSON.stringify(this.toJSONObject()))
        return JSON.stringify(this.toJSONObject());
    }
    toJSONObject(){

        return {"reCode": this.reCode,"reMsg":this.reMsg,"reqSeq":this.reqSeq,"sign":this.sign,"result":this.result,"data":JSON.stringify(this.data)};
    }
}
export function resRep(reports){
    var rr={
        "reCode":reports.reCode,
        "reMsg":reports.reMsg,
        "reqSeq":reports.reqSeq,
        "sign":reports.sign,
        "result":reports.result,
        "data":reports.data
    }
    return new resReport({
        recode:rr.reCode,
        remsg:rr.reMsg,
        reqseq:rr.reqSeq,
        sign:rr.sign,
        result:rr.result,
        d:rr.data
    })
}


/**
 * 获取签名
 * @param report     待签名的报文
 * @param signType   签名方式
 * @returns {string} 签名
 */
function getSign(report,signType){
    let strSign="";
    switch (signType) {
        case "MD5":
            strSign=cSign.md5(report);
            break;
        case "RSA":
            break;
        case "3DES":
            break;
        default:
            strSign=cSign.md5(report);
            break;
    }
    return strSign;
}
